package com.example.servicearea.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.servicearea.domain.DeviceDate;
import com.example.servicearea.domain.PeopleDate;
import com.example.servicearea.domain.VehicleDate;
import com.example.servicearea.service.impl.DeviceService;
import com.example.servicearea.service.impl.PeopleService;
import com.example.servicearea.service.impl.VehicleService;

import com.example.servicearea.utils.ApplicationContextUtils;
import com.example.servicearea.utils.VehicleTypeTransform;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/services")
public class ServiceAreaController {
    @Value("${platform.Ip}")
    private String Ip;
    @Value("${platform.Port}")
    private String Port;


    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DeviceDate deviceDate;
    @Autowired
    private PeopleService peopleService;
    @Autowired
    private VehicleService vehicleService;

    @PostConstruct
    public void initDeviceLogin(){
        VehicleTypeTransform.initVehicleTypeMap();
        List<DeviceDate> deviceDates = deviceService.SelectDeviceInfo(1);

        for (int i = 0; i < deviceDates.size(); i++) {
            Integer deviceState = deviceService.selectDeviceState(i+1);
            if (deviceState == 1){
                String Ip = deviceDates.get(i).getIp();
                String Admin = deviceDates.get(i).getAdmin();
                String Password = deviceDates.get(i).getPassword();
                Integer deviceId = deviceDates.get(i).getDeviceId();
                String deviceType = deviceDates.get(i).getDeviceType();
                deviceService.loginDevice(Ip, Admin, Password, deviceId, deviceType);
            }
            else
                continue;
        }

    }

    @Scheduled(cron = "0 */2 * * * ?")
    public void DeviceStart(){
        Map<String,Integer> map = new HashMap<>();
        Integer serviceId = deviceDate.getServiceId();
        //System.out.println(serviceId);
        map.put("serviceId", serviceId);
        JSONArray result = restTemplate.postForObject("http://" + Ip + ":" + Port + "/Device/deviceBinding",map,JSONArray.class);
        //System.out.println(result);
        for (int i=0;i <result.size();i++){
            HashMap<String,Object> jsonObject = (HashMap) result.get(i);
            Integer deviceId = (Integer) jsonObject.get("serviceLogId");
            Integer deviceState = deviceService.selectDeviceState(deviceId);
            if (deviceState == 1){
                continue;
            }
            else {
                String ip = (String) jsonObject.get("ip");
                String admin = (String) jsonObject.get("admin");
                String password = (String) jsonObject.get("password");
                Integer Id = (Integer) jsonObject.get("deviceId");
                String deviceType = (String) jsonObject.get("deviceType");
                deviceService.loginDevice(ip, admin, password, Id,deviceType);
                deviceService.updateDeviceStateById(deviceId);
            }
        }
    }

    @Scheduled(cron = "0 */2 * * * ?")
    public void SelectDeviceInfo(){

        Integer serviceId = deviceDate.getServiceId();
        String tableName = "device";
        Map<String,Object> Name = new HashMap<>();
        Name.put("tableName",tableName);
        Name.put("serviceId",serviceId);
        Integer maxId = restTemplate.postForObject("http://" + Ip + ":" + Port + "/platform/getMaxServiceLogId",Name,int.class);
        List<DeviceDate> serviceData = deviceService.SelectDeviceInfo(maxId);
        JSONArray result = JSONArray.parseArray(JSON.toJSONString(serviceData));
        Map<String,Object> map = new HashMap<>();
        map.put("tableName",tableName);
        map.put("serviceData",result);
        map.put("serviceId", serviceId);
        restTemplate.postForObject("http://" + Ip + ":" + Port + "/platform/sendServiceAreaData",map,Integer.class);
    }

    @Scheduled(cron = "0 */2 * * * ?")
    public void SelectVehicleInfo(){

        String tableName = "vehicle";
        Integer serviceId = deviceDate.getServiceId();
        Map<String,Object> Name = new HashMap<>();
        Name.put("tableName",tableName);
        Name.put("serviceId",serviceId);
        Integer maxId = restTemplate.postForObject("http://" + Ip + ":" + Port + "/platform/getMaxServiceLogId",Name,int.class);
        System.out.println(maxId);
        List<VehicleDate> serviceData = vehicleService.SelectVehicleById(maxId);
        JSONArray result = JSONArray.parseArray(JSON.toJSONString(serviceData));
        System.out.println(result);
        Map<String,Object> map = new HashMap<>();
        map.put("tableName",tableName);
        map.put("serviceData",result);
        map.put("serviceId", serviceId);
        restTemplate.postForObject("http://" + Ip + ":" + Port + "/platform/sendServiceAreaData",map,Integer.class);
    }

    @Scheduled(cron = "0 */1 * * * ?")
    public void SelectPeopleInfo(){

        String tableName = "peopleflow";
        Integer serviceId = deviceDate.getServiceId();
        Map<String,Object> Name = new HashMap<>();

        Name.put("tableName",tableName);
        Name.put("serviceId",serviceId);
        Integer maxId = restTemplate.postForObject("http://" + Ip + ":" + Port + "/platform/getMaxServiceLogId",Name,int.class);
        List<PeopleDate> serviceData = peopleService.SelectPersonById(maxId);

        JSONArray result = JSONArray.parseArray(JSON.toJSONString(serviceData));
        Map<String,Object> map = new HashMap<>();
        map.put("tableName",tableName);
        map.put("serviceData",result);
        map.put("serviceId", serviceId);
        restTemplate.postForObject("http://" + Ip + ":" + Port + "/platform/sendServiceAreaData",map, Integer.class);
    }

}